<?php

define("COOL_DIAEY", "coolDiary",false);
define("COOL_DIAEY_PRIFIX", "coolDiary_",false);

function coolDiary_showWeather($id) {
			
			$data = get_post_meta($id, 'coolDiary',true);
			
			return '<div id="weather">天气'.$data["city"].' '.$data["weather"].' '.$data["mood"].'</div>';
			
		}


global $_POST;
function getWeatherSimple(){
	$city = $_POST['city'];
	$weather = get_weather($city);

	$data['weather'] = $weather['today']['conditions'];
	$data['city'] = $city;
	$data['mood'] = 'happy';


	echo $data['weather'];
	die();
}

/**
 *
 *Get the vistor ip address
 */
function getIP()
{
	if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
	$ip = getenv("HTTP_CLIENT_IP");
	elseif(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
	$ip = getenv("HTTP_X_FORWARDED_FOR");
	elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
	$ip = getenv("REMOTE_ADDR");
	elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
	$ip = $_SERVER['REMOTE_ADDR'];
	else
	$ip = "0.0.0.0";
	return $ip;
}

/**
 * Get the city name by self-ip.
 * it will return the array like:
 * Array("183.15.217.124","","广东省","深圳市");
 */
function getIPPlace(){
	$ip=mb_convert_encoding(file_get_contents("http://fw.qq.com/ipaddress"), "UTF-8", "GBK");
	$ip=str_replace('"',' ',$ip);
	$ip2=explode("(",$ip);
	$a=substr($ip2[1],0,-2);
	$b=explode(",",$a);
	return $b;
}

/**
 * Get the weather by city name
 * @param $city the city name, it can be chinese or pinyin
 */
/**
 * 函数名:get_weather
 * 作用:从google天气api获得天气情况
 * 返回值:数组
 * pama:    $city
 * type:      char
 * example: 'beijing'
 * 用php Simplexml 调用google天气预报api,和g官方的例子不一样
 * google 官方php domxml 获取google天气预报的例子
 * [url]http://www.google.com/tools/toolbar/buttons/intl/zh-CN/apis/howto_guide.html[/url]
 *
 * 正常情况返回 array('today'=>array(),forecast=>array())
 */
function get_weather ($city='beijing') {

	try{
		$content = file_get_contents("http://maps.google.com/ig/api?weather=$city&hl=zh-cn");
		$content = mb_convert_encoding($content, 'UTF-8', 'GBK');
		if(strpos($content,'problem_cause')) {
			$wt['today'] = array();
			$wt['forecast'] = array();
			return $wt;
		}
		$xml = simplexml_load_string($content);
		$current = $xml->weather->current_conditions;
		$wt['today']['conditions'] = $current->condition->attributes().""; //把对象的值转换成字符串
		$wt['today']['temp_c'] =  $current->temp_c->attributes().""; //温度，摄氏单位
		$wt['today']['wind'] =  $current->wind_condition->attributes().""; //风力风向情况
		$wt['today']['icon'] =  $current->icon->attributes()."";    //天气图标,前面要加上 [url]http://www.google.com[/url]
		foreach($xml->weather->forecast_conditions as $f) {
			$i++;
			$wt['forecast'][$i]['low'] = $f->low->attributes().""; //把对象的值转换成字符串,这个是最低温度
			$wt['forecast'][$i]['high'] = $f->high->attributes()."";    //最高温度
			$wt['forecast'][$i]['icon'] = $f->icon->attributes()."";
			$wt['forecast'][$i]['condition'] = $f->condition->attributes()."";
			$wt['forecast'][$i]['day_of_week'] = $f->day_of_week->attributes()."";  //周五六(*)
		}

	}catch(Exception $e){
		//Exception
		echo  'There is something wrong while get the weather';
	}
	return $wt;//返回的是一个数组。
}




?>